var contentRegisterData;
var editContentRegisterData;

/**
 * 入住管理导航
 */
function registerNav() {
    navSelected('#registerNavItem');
    contentHeadShow('.registerContentHead');
    clearContentTable();
    initRegister();
}

/**
 * 来访导航初始化
 */
function initRegister() {
    $('#contentData').bootstrapTable({
        data: contentRegisterData,
        dataType: 'json',
        pagination: true,
        pageSize: 5,
        striped: true,
        search: false,
        singleSelect: false,
        showHeader: true,
        showFooter: false,
        showColumns: false,
        showRefresh: false,
        showToggle: false,
        sortable: false,
        columns: [{
            field: 'visit_date',
            title: '来访日期',
            align: 'left',
            valign: 'left'
        }, {
            field: 'visitor_name',
            title: '来访者姓名',
            align: 'left',
            valign: 'left'
        }, {
            field: 'visitor_phone',
            title: '来访者电话',
            align: 'left',
            valign: 'left'
        }, {
            field: 'visit_time',
            title: '来访时间',
            align: 'left',
            valign: 'left'
        }, {
            field: 'leave_time',
            title: '离开时间',
            align: 'center',
            valign: 'middle'
        },{
            field: 'visit_reason',
            title: '来访事由',
            align: 'center',
            valign: 'middle'
        },{
            field: 'staff',
            title: '值班人员',
            align: 'center',
            valign: 'middle'
        }, {
            field: 'id',
            title: '操作',
            align: 'center',
            valign: 'middle',
            formatter: registerFormatter
        }]
    });
    initRegisterData();
}

function initRegisterData() {
    $.ajax({
        async: false,
        cache: false,
        type: 'GET',
        dataType: "json",
        accept: "application/json;charset=UTF-8",
        contentType: "application/json;charset=UTF-8",
        url: '/register',
        success: function (data) {
            contentRegisterData = data._embedded.register;
            var table = $('#contentData');
            table.bootstrapTable('refreshOptions', {data: contentRegisterData, dataType: "json"});
        },
        error: function (data) {
        }
    });
}

function registerFormatter(value, row, index) {
    var id = value;
    var result = "";
    result += "<button type='button' class='btn btn-warning' data-toggle='modal' data-target='#registerUpdate' onclick=\"registerUpdate('" + index + "')\"><i class='fa fa-pencil'></i> 修改</button>";
    result += "<button type='button' class='btn btn-danger' onclick=\"registerDelete('" + id + "')\"><i class='fa fa-trash'></i> 删除</button>";
    return result;
}

function registerQuery() {
    var visitDate = $("#visitDate").val();
    // var visitDate = new Date($("#visitDate").val()).toISOString().split('T')[0];
    var visitorName = $("#visitorName").val();

    if (!visitDate) {
        if (!visitorName) {
            // 均为空，初始化查询
            initRegisterData();
        } else {
            // 按照来访者姓名查询
            $.ajax({
                async: false,
                cache: false,
                type: 'GET',
                dataType: "json",
                accept: "application/json;charset=UTF-8",
                contentType: "application/json;charset=UTF-8",
                url: '/register/search/findByVisitorName?visitorName=' + visitorName,
                success: function (data) {
                    contentRegisterData = data._embedded.register;
                    var table = $('#contentData');
                    table.bootstrapTable('refreshOptions', {data: contentRegisterData, dataType: "json"});
                },
                error: function (data) {
                    swal('温馨提示', '按名字查询失败', 'error');
                }
            });
        }
    } else {
        // 按照来访日期查询
        $.ajax({
            async: false,
            cache: false,
            type: 'GET',
            dataType: "json",
            accept: "application/json;charset=UTF-8",
            contentType: "application/json;charset=UTF-8",
            url: '/register/search/findByVisitDate?visitDate=' + encodeURIComponent(visitDate), // 对日期字符串进行URL编码,
            success: function (data) {
                contentRegisterData = data._embedded.register;
                var table = $('#contentData');
                table.bootstrapTable('refreshOptions', {data: contentRegisterData, dataType: "json"});
            },
            error: function (data) {
                swal('温馨提示', '按来访日期查询失败', 'error');
            }
        });
    }
}

function registerAddSave() {
    var data = {};
    data.visit_date = $("#addVisitDate").val();
    data.visitor_name = $("#addVisitorName").val();
    data.visitor_phone = $("#addVisitorPhone").val();
    data.visit_time = $("#addVisitTime").val();
    data.leave_time = $("#addLeaveTime").val();
    data.visit_reason = $("#addVisitReason").val();
    data.staff = $("#addStaff").val();

    $.ajax({
        async: true,
        cache: false,
        type: 'POST',
        data: JSON.stringify(data),
        datType: "json",
        accept: "application/json;charset=UTF-8",
        contentType: "application/json;charset=UTF-8",
        url: '/register',
        success: function (data) {
            swal('温馨提示', '新增来访成功', 'success');
            initRegisterData();
        },
        error: function (data) {
            swal('温馨提示', '新增来访失败', 'error');
        }
    });
}

function registerUpdate(index) {
    var row = contentRegisterData[index];
    editContentRegisterData = contentRegisterData[index];
    $("#updateRegisterId").val(row.id);
    $("#updateVisitDate").val(row.visit_date);
    $("#updateVisitorName").val(row.visitor_name);
    $("#updateVisitorPhone").val(row.visitor_phone);
    $("#updateVisitTime").val(row.visit_time);
    $("#updateLeaveTime").val(row.leave_time);
    $("#updateVisitReason").val(row.visit_reason);
    $("#updateStaff").val(row.staff);

}

function registerUpdateSave() {
    var data = {};
    data.id = $("#updateRegisterId").val();
    data.visit_date = $("#updateVisitDate").val();
    data.visitor_name = $("#updateVisitorName").val();
    data.visitor_phone = $("#updateVisitorPhone").val();
    data.visit_time = $("#updateVisitTime").val();
    data.leave_time = $("#updateLeaveTime").val();
    data.visit_reason = $("#updateVisitReason").val();
    data.staff = $("#updateStaff").val();
    data.createTime = editContentRegisterData.createTime;
    $.ajax({
        async: true,
        cache: false,
        type: 'PUT',
        data: JSON.stringify(data),
        datType: "json",
        accept: "application/json;charset=UTF-8",
        contentType: "application/json;charset=UTF-8",
        url: '/register/' + data.id,
        success: function (data) {
            swal('温馨提示', '修改来访成功', 'success');
            initRegisterData();
        },
        error: function (data) {
            swal('温馨提示', '修改来访失败', 'error');
        }
    });
}

function registerDelete(id) {
    $.ajax({
        async: true,
        cache: false,
        type: 'DELETE',
        datType: "json",
        accept: "application/json;charset=UTF-8",
        contentType: "application/json;charset=UTF-8",
        url: '/register/' + id,
        success: function (data) {
            swal('温馨提示', '删除来访成功', 'success');
            initRegisterData();
        },
        error: function (data) {
            swal('温馨提示', '删除来访失败', 'error');
        }
    });
}

/**
 * 上传Excel文件
 */
function registerUpload() {
    var uploadData = new FormData();
    var uploadName = $("#registerUploadFile").val();
    uploadData.append("file", $("#registerUploadFile")[0].files[0]);
    uploadData.append("name", uploadName);
    $.ajax({
        url: '/excel/import',
        type: 'POST',
        async: false,
        data: uploadData,
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        beforeSend: function () {
            console.log("正在进行，请稍候");
        },
        success: function (data) {
            swal('温馨提示', '导入成功', 'success');
        }
    });
}